/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.biz.jifenlog.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.IdGenerater;
import com.smooth.dao.gen.BizJifenLogMapper;
import com.smooth.dao.gen.bean.BizJifenLog;
import com.smooth.dao.gen.bean.BizJifenLogExample;

/**
 * 积分流水Service
 * @author bm
 * @version 2019-02-22
 */
@Service
@Transactional(readOnly = true)
public class BizJifenService{
	Logger log = LoggerFactory.getLogger(BizJifenService.class);
	@Autowired
	BizJifenLogMapper bizJifenLogMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public BizJifenLog get(String no) {
		return bizJifenLogMapper.selectByPrimaryKey(no);
	}
	
	public List<BizJifenLog> findList(BizJifenLog bizJifenLog) {
		BizJifenLogExample bizJifenLogExample = new BizJifenLogExample();
		BizJifenLogExample.Criteria criteria = bizJifenLogExample.createCriteria();
		
		return bizJifenLogMapper.selectByExample(bizJifenLogExample);
	}
	
	public Page<BizJifenLog> findPage(Page<BizJifenLog> page, BizJifenLog bizJifenLog) {
		BizJifenLogExample bizJifenLogExample = new BizJifenLogExample();
		BizJifenLogExample.Criteria criteria = bizJifenLogExample.createCriteria();
		
		bizJifenLogExample.setPage(page);
		
		List<BizJifenLog> dlist = bizJifenLogMapper.selectByExample(bizJifenLogExample);
		page.setList(dlist);
		page.setCount(bizJifenLogMapper.countByExample(bizJifenLogExample));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public BizJifenLog save(BizJifenLog bizJifenLog) {
		if(StringUtils.isNotEmpty(bizJifenLog.getId())&&get(bizJifenLog.getId())!=null) {
			int optNum = bizJifenLogMapper.updateByPrimaryKeySelective(bizJifenLog);
			if(optNum==1) {
				log.info("更新 student 数据【"+bizJifenLog.toString()+"】成功");
			}else {
				log.error("更新 student 数据【"+bizJifenLog.toString()+"】失败");
			}
			return bizJifenLogMapper.selectByPrimaryKey(bizJifenLog.getId());
		}else {
			Date date = new Date();
			bizJifenLog.setCreateDate(date);
			bizJifenLog.setId(idGenerater.genClsNo("bizJifenLog"));
			bizJifenLog.setUpdateDate(date);
			int optNum =  bizJifenLogMapper.insertSelective(bizJifenLog);
			if(optNum==1) {
				log.info("插入 student 数据【"+bizJifenLog.toString()+"】成功");
			}else {
				log.error("插入 student 数据【"+bizJifenLog.toString()+"】失败");
			}
			return bizJifenLog;
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BizJifenLog bizJifenLog) {
		bizJifenLogMapper.deleteByPrimaryKey(bizJifenLog.getId());
	}
	
}